package com.cskaoyan.market.controller.admin;

import com.cskaoyan.market.db.domain.MarketCoupon;
import com.cskaoyan.market.db.domain.MarketCouponUser;
import com.cskaoyan.market.service.MarketCouponService;
import com.cskaoyan.market.service.impl.MarketCouponServiceImpl;
import com.cskaoyan.market.util.GetRequestBodyUtil;
import com.cskaoyan.market.util.HttpUtil;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@RestController
@RequestMapping("admin/coupon")
public class AdminCouponController{
    @Autowired
    private MarketCouponService marketCouponService;


    @GetMapping("read")
    private Object read(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        MarketCoupon readCoupon = marketCouponService.readCoupon(id);
        return ResponseUtil.ok(readCoupon);
    }

    @GetMapping("list")
    private Object list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String name = req.getParameter("name");
        String type = req.getParameter("type");
        String status = req.getParameter("status");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        List<MarketCoupon> list=marketCouponService.list(page,limit,name,type,status,sort,order);
        System.out.println(list);
        return ResponseUtil.okList(list);
    }


    @GetMapping("listuser")
    private Object listUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String couponId = req.getParameter("couponId");
        String userId = req.getParameter("userId");
        String status = req.getParameter("status");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        List<MarketCouponUser> couponUsers = marketCouponService.listUser(page, limit, couponId, userId, status, sort, order);
        Object o = ResponseUtil.okList(couponUsers);
        return o;
    }

    @PostMapping("create")
    private Object create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = HttpUtil.getRequestBody(req);
        MarketCoupon marketCoupon = JacksonUtil.getObjectMapper().readValue(requestBody, MarketCoupon.class);
        marketCouponService.create(marketCoupon);
        return ResponseUtil.ok(marketCoupon);
    }
    @PostMapping("update")
    private Object update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = GetRequestBodyUtil.getRequestBody(req);
        MarketCoupon marketCoupon = JacksonUtil.getObjectMapper().readValue(requestBody, MarketCoupon.class);
        MarketCoupon updatedCoupon = marketCouponService.updateCoupon(marketCoupon);
        return ResponseUtil.ok(updatedCoupon);
    }
    @PostMapping("delete")
    private Object delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = GetRequestBodyUtil.getRequestBody(req);
        MarketCoupon marketCoupon = JacksonUtil.getObjectMapper().readValue(requestBody, MarketCoupon.class);
        marketCouponService.deleteCoupon(marketCoupon);
        return ResponseUtil.ok();
    }

    //分发请求
    private String getOpString(HttpServletRequest req) {
        String requestURI = req.getRequestURI();
        return requestURI.replace(req.getContextPath() + "/admin/coupon/", "");
    }


}
